package com.saifan.wyy_ov.ui.onlishop;
import java.util.ArrayList;
import java.util.List;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.data.bean.CommentBean;
import com.saifan.wyy_ov.data.bean.MyOrderBean;
import com.saifan.wyy_ov.data.bean.SubmitOrderBean;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.utils.CallPhoneUtil;
import com.saifan.wyy_ov.utils.ImgUtil;
import com.saifan.wyy_ov.utils.PriceUtils;
import com.saifan.wyy_ov.utils.StringUtil;
import com.saifan.wyy_ov.utils.ToastUtil;

public class MyOrderDetailsActivity extends BaseActivity {
	 
	private static final int ORDER = 5544;
	protected static final int DELETEORDER = 1234;
	private MyOrderBean myOrderBean;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Bundle bundle = getIntent().getExtras();
		myOrderBean = (MyOrderBean) bundle.get("myOrderBean");
		bindViews();
		setSupportActionBar(toolbar);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		adapterOrderdetail = new AdapterOrderdetail(this, myOrderBean.getOrderDetail(), myOrderBean);
		lv_order_details.setAdapter(adapterOrderdetail);
		adapterOrderdetail.notifyDataSetChanged();
	}

	// Content View Elements
	private android.support.v7.widget.Toolbar toolbar;
	private TextView tv_userName;
	private TextView tv_userTel;
	private TextView tv_user_address;
	private Button bt_edit_info;
	private com.saifan.wyy_ov.ui.view.CircleImageView iv_store_img;
	private TextView tv_store_name;
	private com.saifan.wyy_ov.ui.view.NoScrollListView lv_order_details;
	private TextView tv_count;
	private TextView tv_total;
	private TextView bt_tel, bt_pay, bt_cancel;
	private TextView tv_orderState;
	private AdapterOrderdetail adapterOrderdetail;
//	private MaterialDialog dialog;
//	private EditText editaddress;
//	private EditText editconcat;
//	private EditText edittel;
	private TextView tv_ordrNumber,tv_order_time;

	// End Of Content View Elements
	private void bindViews() {
		setContentView(R.layout.activity_my_order_details);
		toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
		tv_ordrNumber = (TextView) findViewById(R.id.tv_ordrNumber);
		tv_userName = (TextView) findViewById(R.id.tv_userName);
		tv_userTel = (TextView) findViewById(R.id.tv_userTel);
		tv_user_address = (TextView) findViewById(R.id.tv_address);
		iv_store_img = (com.saifan.wyy_ov.ui.view.CircleImageView) findViewById(R.id.iv_store_img);
		tv_store_name = (TextView) findViewById(R.id.tv_store_name);
		lv_order_details = (com.saifan.wyy_ov.ui.view.NoScrollListView) findViewById(R.id.lv_order_details);
		tv_count = (TextView) findViewById(R.id.tv_count);
		tv_total = (TextView) findViewById(R.id.tv_total);
		bt_tel = (TextView) findViewById(R.id.bt_tel);
		tv_orderState = (TextView) findViewById(R.id.tv_orderState);
		tv_order_time = (TextView) findViewById(R.id.tv_order_time);
		bt_cancel = (Button) findViewById(R.id.bt_cancel);
		tv_ordrNumber.setText("订单编号:  "+myOrderBean.getOrderNumber()+"");
		tv_order_time.setText("订单时间:  "+myOrderBean.getTime());
		bt_pay = (Button) findViewById(R.id.bt_pay);
		ImgUtil.DisPlayImg(myOrderBean.getStoreHeadPhoto(), iv_store_img, this);
		tv_store_name.setText(myOrderBean.getStoreName() + "");
		tv_userName.setText(myOrderBean.getContact() + "");
		tv_count.setText(myOrderBean.getTotal() + "");
		if (myOrderBean.getOrderState() == 1) {
			tv_orderState.setText("待付款");
			bt_cancel.setVisibility(View.VISIBLE);
			bt_pay.setVisibility(View.VISIBLE);
		} else if (myOrderBean.getOrderState() == 2) {
			tv_orderState.setText("待配送");
			bt_cancel.setVisibility(View.GONE);
			bt_pay.setVisibility(View.GONE);
		} else if (myOrderBean.getOrderState() == 3) {
			tv_orderState.setText("配送中");
			bt_cancel.setVisibility(View.GONE);
			bt_pay.setVisibility(View.GONE);
		} else if (myOrderBean.getOrderState() == 4) {
			tv_orderState.setText("退货申请");
			bt_cancel.setVisibility(View.GONE);
			bt_pay.setVisibility(View.GONE);
		} else if (myOrderBean.getOrderState() == 5) {
			tv_orderState.setText("已退款");
			bt_cancel.setVisibility(View.GONE);
			bt_pay.setVisibility(View.GONE);
		} else if (myOrderBean.getOrderState() == 6) {
			tv_orderState.setText("已完成");
			bt_cancel.setVisibility(View.VISIBLE);
			bt_pay.setVisibility(View.VISIBLE);
			bt_pay.setText("评价");
		} else {
			tv_orderState.setText("已取消");
			bt_cancel.setVisibility(View.VISIBLE);
			bt_pay.setVisibility(View.GONE);
		}
		tv_userTel.setText(myOrderBean.getCustomerTel());
		tv_user_address.setText(myOrderBean.getAddress());
		bt_tel.setText("联系商家："+myOrderBean.getStoreTel());
		tv_total.setText(PriceUtils.getPrice(myOrderBean.getTotalPrice())+"元");
		
		lv_order_details.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent = new  Intent(MyOrderDetailsActivity.this,  GoodsDetailActivity.class);
						 intent.putExtra("storeId",  String.valueOf(myOrderBean.getStoreID()));
						 intent.putExtra("goodsId", String.valueOf(myOrderBean.getOrderDetail().get(position).getGoodsID()));
						 intent.putExtra("goodsName", String.valueOf(myOrderBean.getOrderDetail().get(position).getGoodsName()));
						 intent.putExtra("price", String.valueOf(myOrderBean.getOrderDetail().get(position).getPrice()));
						 startActivity(intent);
				
			}
		});
	}




	TextView tel_button;
	public void call(View v) {

		tel_button = (TextView) v;
		if (!StringUtil.isNullOrEmpty(tel_button.getText().toString())) {

			if (Build.VERSION.SDK_INT >= 23) {

				int checkCallPhonePermission = ContextCompat.checkSelfPermission( this, Manifest.permission.CALL_PHONE);
				if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
					ActivityCompat.requestPermissions(MyOrderDetailsActivity.this, new String[]{Manifest.permission.CALL_PHONE}, CALL_PHONE);
					return;
				} else {
					CallPhoneUtil.callPhone(MyOrderDetailsActivity.this, tel_button.getText().toString());


				}
			} else {

				CallPhoneUtil.callPhone(MyOrderDetailsActivity.this, tel_button.getText().toString());

			}


		}

	}



	private static final int CALL_PHONE = 123;
	@Override
	public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
		if (requestCode == CALL_PHONE) {
			if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

				CallPhoneUtil.callPhone(MyOrderDetailsActivity.this, tel_button.getText().toString());


			} else {
				// Permission Denied
				Toast.makeText(this, "您禁用拨打电话权限", Toast.LENGTH_SHORT)
						.show();

			}
		}

	}
	
	
//	public void editInfo(View rootview) {
//		
//		if (dialog != null) {
//			View editView = getLayoutInflater().from(this).inflate(R.layout.contact_edit, null);
//			editaddress = (EditText) editView.findViewById(R.id.edit_address);
//			 
//			editaddress.setText(tv_user_address.getText().toString());
//			editconcat = (EditText) editView.findViewById(R.id.edit_contact);
//			editconcat.setText(tv_userName.getText().toString());
//			edittel = (EditText) editView.findViewById(R.id.edit_tel);
//			edittel.setText(tv_userTel.getText().toString());
//			 
//			
//			dialog.setView(editView).setPositiveButton("是", new View.OnClickListener() {
//				@Override
//				public void onClick(View v) {
//					if (!StringUtil.isNullOrEmpty(editconcat.getText().toString())) {
//						tv_userName.setText(editconcat.getText().toString());
//					}
//					if (!StringUtil.isNullOrEmpty(edittel.getText().toString())) {
//						tv_userTel.setText(edittel.getText().toString());
//					}
//					if (!StringUtil.isNullOrEmpty(editaddress.getText().toString())) {
//						tv_user_address.setText(editaddress.getText().toString());
//					}
//					
//					 
//					dialog.dismiss();
//					
//				 
//
//				}
//			}).setNegativeButton("否", new View.OnClickListener() {
//				@Override
//				public void onClick(View v) {
//					dialog.dismiss();
//
//				}
//			}).setCanceledOnTouchOutside(false)
//
//			.setOnDismissListener(new DialogInterface.OnDismissListener() {
//				@Override
//				public void onDismiss(DialogInterface dialog) {
//
//				}
//			}).show();
//
//		}
//		 
//	}
	
	
	public void cancle(View view) {
		// TODO Auto-generated method stub
		CommentBean commentBean = new CommentBean();
		commentBean.setID(myOrderBean.getID() + "");
		GetData getData = new GetData();
		String tip="正在删除,请稍候！";
		 if (myOrderBean.getOrderState()==1) {
			tip="正在取消,请稍候!";
			getData.getDataJson(this, Api.GroupBuy.CANCELORDER, commentBean, tip, new ResponseData() {
				@Override
				public void onSuccess(String json) {
					// TODO Auto-generated method stub
					 ToastUtil.showToast(MyOrderDetailsActivity.this, "操作成功");
					 	 
					 Intent intent = new Intent();
					 intent.putExtra("OrderID", myOrderBean.getID());
					 MyOrderDetailsActivity.this.setResult(DELETEORDER, intent);
					 finish();
				}

				@Override
				public void onFailed(String str) {
					// TODO Auto-generated method stub
					ToastUtil.showToast(MyOrderDetailsActivity.this, "操作失败");
					 
				}
			});
		}else if(myOrderBean.getOrderState()==6||myOrderBean.getOrderState()==7) {
			tip="正在取消,请稍候!";
			getData.getDataJson(this, Api.GroupBuy.DELETEORDER, commentBean, tip, new ResponseData() {
				@Override
				public void onSuccess(String json) {
					// TODO Auto-generated method stub
					 ToastUtil.showToast(MyOrderDetailsActivity.this, "操作成功");
					 	 
					 Intent intent = new Intent();
					 intent.putExtra("OrderID", myOrderBean.getID());
					 MyOrderDetailsActivity.this.setResult(DELETEORDER, intent);
					 finish();
				}

				@Override
				public void onFailed(String str) {
					// TODO Auto-generated method stub
					ToastUtil.showToast(MyOrderDetailsActivity.this, "操作失败");
					 
				}
			});
			
		}
		
		
	}

	public void pay(View view) {
		// TODO Auto-generated method stub
		// 未付款
		if (myOrderBean.getOrderState() == 1) {
			GetData getData = new GetData();
			List<SubmitOrderBean> submitOrderBeans = new ArrayList<SubmitOrderBean>();
			SubmitOrderBean submitOrderBean = new SubmitOrderBean();
			int paymentWay = myOrderBean.getPaymentWay();
			double total = Double.valueOf(myOrderBean.getTotalPrice());
			submitOrderBean.setAmount(total + "");
			submitOrderBean.setID(myOrderBean.getID());
			submitOrderBean.setOrderNumber(myOrderBean.getOrderNumber());
			submitOrderBeans.add(submitOrderBean);
			Intent intentGoPayActivity = new Intent(this, PayActivity.class);
			intentGoPayActivity.putExtra("paymentWay", paymentWay);
			intentGoPayActivity.putExtra("total", total);
			intentGoPayActivity.putExtra("SubmitOrderBeans", new Gson().toJson(submitOrderBeans));
			finish();
			startActivity(intentGoPayActivity);
		}
		// 已付款 评价
		else if (myOrderBean.getOrderState() == 6) {
			Intent intent = new Intent(this, SubmitCommentsActivity.class);
			intent.putExtra("StoreID", myOrderBean.getStoreID());
			finish();
			startActivity(intent);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == android.R.id.home) {
			 
			 finish();
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		  if (keyCode== KeyEvent.KEYCODE_BACK) {
			    finish();
			
		}
		return false;
	}
}
